package com.zzzzzz.content.content;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzzzzz.account.signin.NoSuchOnlineUserException;
import com.zzzzzz.account.signin.OnlineService;
import com.zzzzzz.common.page.PageStart;
import com.zzzzzz.content.Constants;
import com.zzzzzz.content.tag.NoSuchTagException;
import com.zzzzzz.content.tag.TagDao;
import com.zzzzzz.content.userfeel.NoSuchUserFeelException;
import com.zzzzzz.content.userfeel.UserFeelAlreadyExistsException;
import com.zzzzzz.core.web.BaseController;

@Controller
public class ContentController extends BaseController {
	@Resource
	private ContentService contentService;
	@Resource
	private OnlineService onlineService;
	@Resource
	private TagDao tagDao;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String list(@RequestParam(value = "start", defaultValue = "0", required = false) Integer start, HttpServletRequest request, Model model) throws NoSuchTagException {
		List<ContentListView> contentList = contentService.find("add_dt desc", start, PageStart.DEFAULT_OFFSET);
		Long contentCount = contentService.findCount();
		PageStart pageStart = new PageStart(start, PageStart.DEFAULT_OFFSET, contentCount, "/");
		
		for(ContentListView contentListView : contentList){
			String picId = contentListView.getPicId();
			if(StringUtils.isNotBlank(picId)){
				String[] picIds = StringUtils.split(picId, ",");
				String[] picIdsShow = null;
				if(picIds.length >= 3){
					picIdsShow = new String[3];
					picIdsShow[0] = picIds[0];
					picIdsShow[1] = picIds[1];
					picIdsShow[2] = picIds[2];
				}else if(picIds.length < 3){
					picIdsShow = new String[1];
					picIdsShow[0] = picIds[0];
				}
				
				contentListView.setPicIds(picIdsShow);
				contentListView.setPicIdsCount(picIdsShow.length);
			}
		}
		
		model.addAttribute("contentList", contentList);
		model.addAttribute("count", contentCount);
		model.addAttribute("pageBar", pageStart.getPageBar());
		
		return "content/list";
	}
	
	@RequestMapping(value = "/tag/{tagName}", method = RequestMethod.GET)
	public String listByTagId(@PathVariable String tagName, @RequestParam(value = "start", defaultValue = "0", required = false) Integer start, HttpServletRequest request, Model model) throws NoSuchTagException {
		Integer tagId = tagDao.findIdByName(tagName);
		
		List<ContentListView> contentList = contentService.findByTagId(tagId, "add_dt desc", start, PageStart.DEFAULT_OFFSET);
		Long contentCount = contentService.findByTagIdCount(tagId);
		PageStart pageStart = new PageStart(start, PageStart.DEFAULT_OFFSET, contentCount, "/tag/" + tagName);
		
		for(ContentListView contentListView : contentList){
			String picId = contentListView.getPicId();
			if(StringUtils.isNotBlank(picId)){
				String[] picIds = StringUtils.split(picId, ",");
				String[] picIdsShow = null;
				if(picIds.length >= 3){
					picIdsShow = new String[3];
					picIdsShow[0] = picIds[0];
					picIdsShow[1] = picIds[1];
					picIdsShow[2] = picIds[2];
				}else if(picIds.length < 3){
					picIdsShow = new String[1];
					picIdsShow[0] = picIds[0];
				}
				
				contentListView.setPicIds(picIdsShow);
				contentListView.setPicIdsCount(picIdsShow.length);
			}
		}
		
		model.addAttribute("contentList", contentList);
		model.addAttribute("count", contentCount);
		model.addAttribute("pageBar", pageStart.getPageBar());
		
		return "content/list";
	}
	
	@RequestMapping(value = "/content/like/{idType}/{id}", method = RequestMethod.GET)
	public @ResponseBody Integer like(@PathVariable Integer idType, @PathVariable Long id) throws NoSuchOnlineUserException {
		Long userId = onlineService.findUserId();
		try {
			return contentService.like(id, idType, userId);
		} catch (UserFeelAlreadyExistsException e) {
			e.printStackTrace();
			return Constants.FEEL_RESULT.ALREADY_FEEL;
		}
	}
	
	@RequestMapping(value = "/content/unfeel/{idType}/{id}", method = RequestMethod.GET)
	public @ResponseBody Integer feelNoLike(@PathVariable Integer idType, @PathVariable Long id) throws NoSuchOnlineUserException {
		Long userId = onlineService.findUserId();
		try {
			return contentService.unFeel(id, idType, userId);
		} catch (NoSuchUserFeelException e) {
			e.printStackTrace();
			return Constants.FEEL_RESULT.NOSUCH_FEEL;
		}
	}
	
}
